package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/twpayne/go-geom/encoding/geojson"
	"github.com/twpayne/go-geom/encoding/wkt"
	"net/http"
	"server/conf"
	"server/models"
)

type Item struct {
	Name   string `json:"name"`
	Value  int    `json:"value"`
	Serial string `json:"serial"`
}

func Data(c *gin.Context) {
	var result []struct {
		Name  string
		Total int
	}
	var context []Item
	db := conf.InitDb()

	db.Model(&models.DataNetworkActivity{}).Select("type_activity as name, count(type_activity) as total").Group("type_activity").Find(&result)
	for _, v := range result {
		context = append(context, Item{Name: v.Name, Value: v.Total, Serial: "类型"})
	}
	c.JSON(http.StatusOK, context)
}

func Geom(c *gin.Context) {
	var result []struct {
		Name string `json:"name"`
		Geom string `json:"geom"`
	}
	//var context []Item
	db := conf.InitDb()
	var context []*geojson.Feature
	db.Model(&models.DataNetworkActivity{}).Select("nm_activity as name, st_AsText(geom_mars) as geom").Find(&result)
	for _, v := range result {
		unmarshal, err := wkt.Unmarshal(v.Geom)

		if err != nil {
			return
		}

		context = append(context, &geojson.Feature{Geometry: unmarshal})

	}
	c.JSON(http.StatusOK, geojson.FeatureCollection{Features: context})
}
